package com.hnd.springframework.beans.factory.support;

import com.hnd.springframework.beans.BeansException;
import com.hnd.springframework.beans.factory.BeanFactory;
import com.hnd.springframework.beans.factory.config.BeanDefinition;

public abstract class AbstractBeanFactory extends DefaultSinletonBeanRegisty implements BeanFactory {

    @Override
    public Object getBean(String name) {
        return getBean(name,new Object[0]);
    }

    @Override
    public Object getBean(String beanName,Object... args) {
        Object bean = getSingletonBean(beanName);

        if (bean != null) {
            return bean;
        }

        BeanDefinition beanDefinition = getBeanDefinition(beanName);
        bean = createBean(beanName, beanDefinition,args);

        if (BeanDefinition.SINGLETON.equals(beanDefinition.getBeanType())) {
            addSinleton(beanName,bean);
        }

        return bean;
    }

    abstract Object createBean(String beanName, BeanDefinition beanDefinition,Object... args) throws BeansException;

    abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException;
}
